var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "services/shopping", "react", "maishu-dilu", "maishu-wuzhui", "maishu-wuzhui-helper", "react-dom", "maishu-ui-toolkit", "services/service", "controls/item-dialog"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    const shopping_1 = require("services/shopping");
    const React = require("react");
    const maishu_dilu_1 = require("maishu-dilu");
    const wuzhui = require("maishu-wuzhui");
    const maishu_wuzhui_helper_1 = require("maishu-wuzhui-helper");
    const ReactDOM = require("react-dom");
    const ui = require("maishu-ui-toolkit");
    const service_1 = require("services/service");
    const item_dialog_1 = require("controls/item-dialog");
    let shopping = service_1.Service.createInstance(shopping_1.ShoppingService);
    class SolutionListPage extends React.Component {
        constructor(props) {
            super(props);
            this.items = new Array();
        }
        componentDidMount() {
            let dataSource = this.dataSource = new wuzhui.DataSource({
                select: () => __awaiter(this, void 0, void 0, function* () {
                    let r = yield shopping.freightSolutions();
                    return { dataItems: r, totalRowCount: r.length };
                }),
                delete: (dataItem) => shopping.deleteFreightSolution(dataItem),
                update: (dataItem) => shopping.updateFreightSolution(dataItem),
                insert: (dataItem) => shopping.addFreightSolution(dataItem),
                primaryKeys: ['Id']
            });
            dataSource.selected.add((sender, result) => {
                this.items = result.dataItems;
            });
            let it = this;
            let gridView = maishu_wuzhui_helper_1.createGridView({
                element: this.gridViewElement,
                columns: [
                    new wuzhui.BoundField({
                        dataField: 'Id', headerText: '编号',
                        headerStyle: { width: '300px' }
                    }),
                    new wuzhui.BoundField({
                        dataField: 'Name', headerText: '名称',
                    }),
                    new wuzhui.CustomField({
                        createItemCell(dataItem) {
                            let cell = new IsDefaultCell(it, dataItem);
                            return cell;
                        },
                        itemStyle: { textAlign: 'center', width: '100px' },
                        headerText: '是否默认'
                    }),
                    new wuzhui.CustomField({
                        createItemCell(dataItem) {
                            let cell = new wuzhui.GridViewCell();
                            ReactDOM.render(React.createElement("div", null,
                                React.createElement("button", { className: "btn btn-info btn-minier", style: { marginRight: 4 }, onClick: () => it.showDialog(dataItem) },
                                    React.createElement("i", { className: "icon-pencil" })),
                                React.createElement("button", { className: "btn btn-minier btn-danger", style: { marginRight: 4 }, ref: (e) => {
                                        if (!e)
                                            return;
                                        e.onclick = ui.buttonOnClick(() => dataSource.delete(dataItem), { confirm: `确定要删除'${dataItem.Name}'运费方案吗` });
                                    } },
                                    React.createElement("i", { className: "icon-trash" })),
                                React.createElement("a", { className: "btn btn-info btn-minier", style: { marginRight: 4 }, href: `#freight/freight-list?id=${dataItem.Id}&name=${encodeURI(dataItem.Name)}` }, "\u8BBE\u7F6E\u8FD0\u8D39")), cell.element);
                            return cell;
                        },
                        headerText: '操作',
                        headerStyle: { width: '180px' },
                        itemStyle: { textAlign: 'center' },
                    })
                ],
                dataSource,
                pageSize: null
            });
        }
        showDialog(dataItem) {
            dataItem = dataItem || {};
            let title = dataItem ? '编辑运费模板' : '添加运费模板';
            this.solutionDialog.show(dataItem, title, (item) => {
                if (!item.Id)
                    this.dataSource.insert(item);
                else
                    this.dataSource.update(item);
            });
        }
        render() {
            return React.createElement(React.Fragment, null,
                React.createElement("div", { className: "tabbable" },
                    React.createElement("ul", { className: "nav nav-tabs" },
                        React.createElement("li", { className: "pull-right" },
                            React.createElement("button", { "data-bind": "click:newItem", className: "btn btn-primary btn-sm", onClick: () => this.showDialog() },
                                React.createElement("i", { className: "icon-plus" }),
                                React.createElement("span", null, "\u65B0\u5EFA\u8FD0\u8D39\u6A21\u677F"))))),
                React.createElement("table", { ref: (e) => this.gridViewElement = e || this.gridViewElement }),
                React.createElement(SolutionDialog, { ref: e => this.solutionDialog = e || this.solutionDialog }));
        }
    }
    exports.default = SolutionListPage;
    class IsDefaultCell extends wuzhui.GridViewDataCell {
        constructor(solutionListPage, dataItem) {
            super({ render: null });
            this.dataField = 'IsDefault';
            this.solutionListPage = solutionListPage;
            this.dataItem = dataItem;
        }
        render(value) {
            ReactDOM.render(value != true ?
                React.createElement("button", { className: "btn btn-minier btn-primary", ref: (e) => {
                        if (!e)
                            return;
                        ui.buttonOnClick(e, () => __awaiter(this, void 0, void 0, function* () {
                            let shopping = service_1.Service.createInstance(shopping_1.ShoppingService);
                            let dataItem = this.dataItem;
                            let it = this.solutionListPage;
                            let result = yield shopping.setDefaultFreightSolution(dataItem.Id);
                            dataItem.IsDefault = true;
                            it.items.forEach(item => {
                                item.IsDefault = item == dataItem;
                                it.dataSource.updated.fire(it.dataSource, item);
                            });
                            return result;
                        }));
                    } }, "\u8BBE\u4E3A\u9ED8\u8BA4 ") :
                React.createElement("span", null, "\u9ED8\u8BA4\u65B9\u6848"), this.element);
        }
    }
    class SolutionDialog extends item_dialog_1.ItemDialog {
        constructor() {
            super(...arguments);
            this.NAME = "Name";
        }
        componentDidMount() {
            this.validator = new maishu_dilu_1.FormValidator(this.element, { name: this.NAME, rules: [maishu_dilu_1.rules.required("请输入运费模板的名称")] });
        }
        render() {
            let { item: dataItem } = this.state;
            return React.createElement(React.Fragment, null,
                React.createElement("div", { className: "form-group clearfix" },
                    React.createElement("label", { className: "col-sm-2 control-label" }, "\u540D\u79F0*"),
                    React.createElement("div", { className: "col-sm-10" },
                        React.createElement("input", { name: this.NAME, type: "text", className: "form-control", placeholder: "\u8BF7\u8F93\u5165\u8FD0\u8D39\u6A21\u677F\u7684\u540D\u79F0", value: dataItem.Name || "", onChange: (e) => {
                                dataItem.Name = e.target.value;
                                this.setState({ item: dataItem });
                            } }))),
                React.createElement("div", { className: "form-group clearfix" },
                    React.createElement("label", { className: "col-sm-2 control-label" }, "\u9ED8\u8BA4\u8FD0\u8D39"),
                    React.createElement("div", { className: "col-sm-10" },
                        React.createElement("input", { name: "freight", type: "text", className: "form-control", placeholder: "\u5728\u8BE5\u6A21\u677F\u4E0B\uFF0C\u9002\u7528\u4E8E\u5927\u90E8\u5206\u5730\u533A\u7684\u8FD0\u8D39", onChange: (e) => {
                                if (!maishu_dilu_1.rules.numeric().validate(e.target.value))
                                    return;
                                dataItem.DefaultFreight = Number.parseFloat(e.target.value);
                            } }))));
        }
    }
});
